package org.soderiakelvy.model.service.imp;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.soderiakelvy.model.dao.RolDao;
import org.soderiakelvy.model.dao.imp.RolDaoImp;
import org.soderiakelvy.model.domain.RolVO;
import org.soderiakelvy.model.service.LoginServicio;
import org.soderiakelvy.model.service.RepartoServicio;
import org.soderiakelvy.model.util.ConstantModel;

public class LoginServicioImpl implements LoginServicio, Serializable{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    private RolDao rolDao;

   
    //--- CONSTRUCTOR ----
    public LoginServicioImpl(){		
        rolDao = new RolDaoImp();
    }
	
	//-----------METODOS PARA ROLES-----------------------

	@Override
	public String insertRolVO(RolVO rol) {
		try {
			rolDao.insertRolVO(rol);
			return ConstantModel.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error al guardar un Rol "+e.getMessage());
			return ConstantModel.FAILED;
		}
	}

	@Override
	public String updateRolVO(RolVO rol) {
		try {
			rolDao.updateRolVO(rol);
			return ConstantModel.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error al Modificar un Rol "+e.getMessage());
			return ConstantModel.FAILED;
		}
	}

	@Override
	public String deleteRolVO(RolVO rol) {
		try {
			RepartoServicio repartoServicio = new RepartoServicioImp();
			if (repartoServicio.getPersonalByRol(rol) == null) {
				rolDao.deleteRolVO(rol);
				return ConstantModel.SUCCESS;	
			}else{
				System.out.println("Error al Eliminar un Rol esta asignado a algun personal" );
				return ConstantModel.FAILED;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error al Eliminar un Rol "+e.getMessage());
			return ConstantModel.FAILED;
		}
	}

	@Override
	public List<RolVO> getAllRoles() {
		try {
			return rolDao.getAllRoles();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error al al listar los Roles "+e.getMessage());
			return null;
		}
	}


	@Override
	public RolVO getRolByRolId(Integer rolId) {
		try {
			return rolDao.getRolVOByRolId(rolId);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error al Obtener Rol "+e.getMessage());
		}
		return null;
	}
	
	@Override
	public Boolean existeNombreRol(String nombre) {
		try{
			if (rolDao.getRolVOByNombre(nombre) != null){
				return true;
			}else
				return false;
		}catch (Exception e){
			e.printStackTrace();
			System.out.println("Error al buscar nombre de Rol. "+e.getMessage());
			return null;	
		}
	}

}
